##################################################################
# File : /lua/ui/game/worldview.lua
# Description : WorldView module
# Author : GPG / Neruz
##################################################################

local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local Group = import('/lua/maui/group.lua').Group

View = false

function Create()
    if View then
        View:Destroy()
        View = false
    end

    View = import('/lua/ui/controls/worldview.lua').WorldView(GetFrame(0), 'WorldCamera', 1, false) # depth value should be below minimap
    View:SetRenderPass(UIUtil.UIRP_UnderWorld | UIUtil.UIRP_PostGlow) # don't change this or the camera will lag one frame behind
    LayoutHelpers.FillParent(View, GetFrame(0))
    View:GetsGlobalCameraCommands(true)
    
    SyncCallback:Add(
        function()
            if Sync.LockInput then
                import('/lua/ui/game/worldview.lua').LockInput()
            end
            if Sync.UnlockInput then
                import('/lua/ui/game/worldview.lua').UnlockInput()
            end
        end
    )

    return View
end

function Destroy()
    if View then
        View:Destroy()
        View = false
    end
end

local worldBlock = false

function LockInput()
    if not worldBlock then
        worldBlock = Group(GetFrame(0))
        LayoutHelpers.FillParent(worldBlock, GetFrame(0))
        UIUtil.MakeInputModal(worldBlock)
    end
    
    if View then
        View:LockInput()
    end
    
    SessionResume()
end

function UnlockInput()
    if View then
        View:UnlockInput()
    end
    
    if not IsInputLocked() then
        if worldBlock then
            worldBlock:Destroy()
            worldBlock = false
        end
    end
end

function IsInputLocked()
    return (View and View:IsInputLocked())
end

function SetHighlightMode(mode)
    if View then
        View:SetHighlightMode(mode)
    end
end
